package com.um.jdy.platform.shop.manager;


import cn.hutool.core.bean.BeanUtil;
import com.um.jdy.common.base.entity.po.Area;
import com.um.jdy.common.base.enums.YesOrNo;
import com.um.jdy.common.base.service.AreaService;
import com.um.jdy.common.shop.service.DeliveryTemplateService;
import com.um.jdy.common.shop.entity.po.DeliveryTemplate;
import com.um.jdy.common.shop.entity.dto.DeliveryTemplateDTO;
import com.um.jdy.common.shop.entity.vo.DeliveryTemplateVO;
import com.um.jdy.platform.base.entity.vo.AreaTreeNode;
import com.um.jdy.platform.shop.entity.dto.DeliveryTemplateListDTO;
import com.um.jdy.platform.shop.entity.dto.DeliveryTemplatePageDTO;
import com.um.springboot.starter.exception.ServiceException;
import com.um.springboot.starter.orm.entity.Response;
import com.um.springboot.starter.orm.manager.BasePlusManager;
import com.um.springboot.starter.orm.params.ListDTO;
import com.um.springboot.starter.utils.StringUtil;
import com.um.springboot.starter.utils.UMBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 管理类：运费模板表
 */
@Component
public class DeliveryTemplateManager extends BasePlusManager<DeliveryTemplateService, DeliveryTemplate, DeliveryTemplateDTO, DeliveryTemplateVO, DeliveryTemplatePageDTO, DeliveryTemplateListDTO> {
    @Autowired
    AreaService areaService;

    public List<AreaTreeNode> getAreaData() {
        List<Area> listProvince = areaService.getList(new String[]{"deep"}, new Object[]{"1"}, new String[]{"sort_no"}, new String[]{"asc"});
        List<Area> listCity = areaService.getList(new String[]{"deep"}, new Object[]{"2"}, new String[]{"sort_no"}, new String[]{"asc"});
        List<Area> listArea = areaService.getList(new String[]{"deep"}, new Object[]{"3"}, new String[]{"sort_no"}, new String[]{"asc"});

        Map<String, List<AreaTreeNode>> mapCityChildren = new Hashtable<>();
        for (Area area : listArea) {

            AreaTreeNode areaNode = BeanUtil.copyProperties(area, AreaTreeNode.class);
            String parentId = areaNode.getBaseAreaParentId();

            List list = null;
            if (mapCityChildren.containsKey(parentId)) {
                list = mapCityChildren.get(parentId);
            } else {
                list = new ArrayList();
                mapCityChildren.put(parentId, list);
            }
            list.add(areaNode);
        }

        Map<String, List<AreaTreeNode>> mapProvinceChildren = new Hashtable<>();

        for (Area city : listCity) {
            AreaTreeNode cityNode = BeanUtil.copyProperties(city, AreaTreeNode.class);
            String id = city.getBaseAreaId();
            if (mapCityChildren.containsKey(id)) {
                cityNode.setChildren(mapCityChildren.get(id));
            }

            String parentId = cityNode.getBaseAreaParentId();
            List list = null;
            if (mapProvinceChildren.containsKey(parentId)) {
                list = mapProvinceChildren.get(parentId);
            } else {
                list = new ArrayList();
            }
            list.add(cityNode);
            mapProvinceChildren.put(parentId, list);
        }

        List<AreaTreeNode> returnList = new ArrayList<>();
        for (Area province : listProvince) {
            AreaTreeNode provinceNode = BeanUtil.copyProperties(province, AreaTreeNode.class);

            String id = provinceNode.getBaseAreaId();
            if (mapProvinceChildren.containsKey(id)) {
                provinceNode.setChildren(mapProvinceChildren.get(id));
            } else {
                provinceNode.setChildren(new ArrayList<>());
            }
            returnList.add(provinceNode);
        }

        return returnList;
    }



    @Transactional
    @Override
    public Response<DeliveryTemplate> addEntity(DeliveryTemplateDTO dto) {

        if(StringUtil.equals(dto.getIsDefault(),YesOrNo.Yes.name())) {
            DeliveryTemplate defaultEntity = service.getEntity(new String[]{"is_default"}, new Object[]{YesOrNo.Yes.name()});
            if (defaultEntity != null) {
                defaultEntity.setIsDefault(YesOrNo.No.name());
                service.update(defaultEntity);
            }
        }

        DeliveryTemplate entity = UMBeanUtils.copyBean(dto, DeliveryTemplate.class);
        service.addEntity(entity);



        return Response.success(BeanUtil.toBean(dto, DeliveryTemplate.class));
    }

    @Transactional
    @Override
    public Response<DeliveryTemplate> updateEntity(String id, DeliveryTemplateDTO dto) {

        DeliveryTemplate defaultEntity = service.getEntity(new String[]{"is_default"}, new Object[]{YesOrNo.Yes.name()});
        if(StringUtil.equals(dto.getIsDefault(),YesOrNo.Yes.name()) && defaultEntity != null && !defaultEntity.getShopDeliveryTemplateId().equals(id)) {
            defaultEntity.setIsDefault(YesOrNo.No.name());
            service.update(defaultEntity);
        }

        DeliveryTemplate entity = UMBeanUtils.copyBean(dto,DeliveryTemplate.class);
        entity.setShopDeliveryTemplateId(id);
        service.updateEntity(entity);
        return Response.success(BeanUtil.toBean(dto, DeliveryTemplate.class));
    }
}
